#!/usr/bin/env python

import hashlib

def md5crypt ( txt ):
	"""
	md5crypt ( txt ) -> string

	This is just a commodity to get the md5 hexdigest string from
	a ``txt`` string provided.
	"""
	if isinstance ( txt, unicode ):
		txt = txt.encode ( 'utf-8' )

	m = hashlib.md5 ()
	m.update ( txt )
	return m.hexdigest ()

def tmpnam ( prefix = '', rand = '', ext = 'tmp' ):
	"""
	tmpnam ( prefix = '', rand = '', ext = 'tmp' ) -> string

	This function creates a random filename.
	You can specify a string ``prefix`` to be added before the random string
	and, to add some more randomization to the process a ``rand`` string.

	By default, the file will have the extension ``ext`` ('tmp'), but you
	can change it if you want.

	It returns a new string containing your ``tmpnam``.

	Requires: :module:hashlib, :module:time and :module:os
	"""
	import time, os

	m = hashlib.md5 ()
	m.update ( "%s" % time.time () )
	m.update ( "%s" % os.urandom ( 255 ) )
	m.update ( "%s" % os.getpid () )
	m.update ( "%s" % rand )

	fname = "%s%s.%s" % ( prefix, m.hexdigest (), ext )
	return fname


